//
//  MyViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/8.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "RDVTabBarController.h"
#import "MyViewController.h"
#import "TheLoginViewController.h"
#import "ToBindCompanyViewController.h"
#import "HttpRequest.h"
#import "RDVTabBarController.h"
#import "UIImageView+WebCache.h"
#import "AboutUsViewController.h"
#import "UIButton+WebCache.h"
#import <RongIMKit/RongIMKit.h>
#import "AccountManagementViewController.h"
#import "DiscoverHouseholdMyCollectViewController.h"
#import "MyHeaderView.h"
#import "MemberListModel.h"
#import "MyresultsViewController.h"
#import "SystemSettingsViewController.h"
#import "LoginCompanysViewController.h"
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate, UITextViewDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,MyHeaderViewDelegate,AccountManagementViewControllerDelegate, RCIMConnectionStatusDelegate,
RCIMReceiveMessageDelegate>

{
    MyHeaderView*HeaderView;
    NSArray *array;
    NSArray *array1;
    NSArray *array2;
    NSArray *array3;
    NSDictionary*dic;
    UITableView*table;
    NSDictionary *DataLine;
    UILabel *how;
    UILabel*line;
}

@end

@implementation MyViewController
@synthesize alt = alt;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"我的";
        
    }
    return self;
}
- (BOOL)prefersStatusBarHidden
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    // 已经不起作用了
    return YES;
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:NO];
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBarHidden=YES;
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.navigationController.delegate = self;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 0)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeToken) name:@"changeTokenId" object:Nil];
    [line removeFromSuperview];
   line=[[UILabel alloc] initWithFrame:CGRectMake(0,ScreenHeight-49.35,ScreenWidth, 0.40)];
    line.backgroundColor=qianhuiColor;
    [self.view addSubview:line];

}
-(void)ChangeToken{
    
    [self requestData];
    [self tableHeadView];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    NSArray *arr1 = @[@"我的业绩",@"wodeyeji"];
    NSArray *arr2 = @[@"我的收藏",@"wodeshoucang"];
    NSArray *arr3 = @[@"所属门店",@"suoshumendian"];
//    NSArray *arr4 = @[@"关于我们",@"gongnengjieshao"];
    NSArray *arr5 = @[@"系统设置",@"xitongshezhi"];
    array = @[arr1,arr2,arr3,arr5];
//    array1 = @[arr3,arr4];
    [self requestData];
    [self tableHeadView];
   }
//-(void)update:(NSString *)updateData{
// [self requestData];
//}
-(void)requestData{
    
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    self.title =@"";
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
    [HttpRequest requestData1:@"Common/GetUserAuthStatus" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
//                NSLog(@"%@",severDataDict.dataDict);
                [Global hideProgressHUDInternal:self.view];
                dic=severDataDict.dataDict;
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                
                [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"]objectForKey:@"fid" ] forKey:@"fid"];

                [user setObject:[severDataDict.dataDict objectForKey:@"headImageUrl"] forKey:@"headImageUrl"];
                [user setObject:[severDataDict.dataDict objectForKey:@"headImage"] forKey:@"headImage"];
                [user setObject:[severDataDict.dataDict objectForKey:@"sex"] forKey:@"sex"];
                [user setObject:[severDataDict.dataDict objectForKey:@"userName"]forKey:@"userName"];
                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"companyName"]]forKey:@"companyName"];
                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"serverAddress"]] forKey:@"serverAddress"];
                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"userStatus"]] forKey:@"userStatus"];
                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"companyId"]] forKey:@"companyId"];
                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"userTypeName"]]  forKey:@"userTypeName"];
                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"userCode"]]  forKey:@"userCode"];
                [user setObject:[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"groupId" ]] forKey:@"groupId"];
                [user synchronize];
               MemberListModel *dataModel=[MemberListModel dataModelWithUserDict:dic];
                HeaderView.model=dataModel;
                [table reloadData];
                if (Token!=nil) {
                }else{
                    
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        TheLoginViewController*detailViewController = [[TheLoginViewController alloc] init];
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    }
                }
            }
            else{
                [Global hideProgressHUDInternal:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
    }];
    
    
}
- (void)tableHeadView
{
    [table removeFromSuperview];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    HeaderView = [[MyHeaderView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,322/2)];
    HeaderView.mDelegate=self;
    table.tableHeaderView = HeaderView;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token==nil) {
        
    }else{
        
//        UIView * tableFooterViews=[[UIView alloc]initWithFrame:CGRectMake(0, -25, ScreenWidth, 200)];
//        tableFooterViews.backgroundColor=zuiqianhuiColor;
//        UIView*huiseview=[[UIView alloc]initWithFrame:CGRectMake(0,20, ScreenWidth, 50)];
//        huiseview.backgroundColor=[UIColor whiteColor];
//        huiseview.layer.borderWidth = 0.3;
//        huiseview.layer.borderColor = qianhuiColor.CGColor;
//        [tableFooterViews addSubview:huiseview];
//        UIButton*exit=[[UIButton alloc]init];
//        exit.backgroundColor=[UIColor clearColor];
//        [exit setTitle:@"退出登录" forState:UIControlStateNormal];
//        exit. titleLabel.font=[UIFont systemFontOfSize:16];
//        [exit setTitleColor:shenhongseColor forState:UIControlStateNormal];
//        exit.frame=CGRectMake(0, 0, ScreenWidth, 50);
//        [exit addTarget:self action:@selector(exit:) forControlEvents:UIControlEventTouchUpInside];
//        [huiseview addSubview:exit];
//        table.tableFooterView = tableFooterViews;
        
    }
    
    table.backgroundColor=zuiqianhuiColor;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
}



-(void)certificationBtn:(NSString*)certificationBtn{
    
    ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
    Login.viewSat=@"The original road";
    [self.navigationController pushViewController:Login animated:YES];
}
-(void)store:(NSString*)store{
    
    AccountManagementViewController * Login=[[AccountManagementViewController alloc]init];
    
    [self.navigationController pushViewController:Login animated:YES];
}

#pragma mark--TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return array.count;}
    //    }if (section == 1) {
    //        return array1.count;
    //    }
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.font=[UIFont systemFontOfSize:15];
    cell.textLabel.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1]
    ;
    if (indexPath.section == 0) {
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-40,10, 30, 30);
        [jiantou setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];
        
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[array objectAtIndex:indexPath.row] objectAtIndex:1]]];
        cell.textLabel.text = [[array objectAtIndex:indexPath.row] objectAtIndex:0];
        if ([[[array objectAtIndex:indexPath.row] objectAtIndex:0]isEqualToString:@"所属门店"]) {
            [how removeFromSuperview];
            how=[[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-50, 0, 50, 50)];
             NSString*companyQty=[NSString stringWithFormat:@"%@",[dic objectForKey:@"companyQty"]];
            
            NSLog(@"======%@",companyQty);
            if ([[NSString stringWithFormat:@"%@",[dic objectForKey:@"companyQty"]]isEqualToString:@"(null)"]) {
                
            }else{
                how.text=companyQty;

            }
            
           how.font=[UIFont systemFontOfSize:14];
            how.textColor=shenhuiColor;
            [cell.contentView addSubview:how];
        }
        
        if (indexPath.row==0) {
            UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.30)];
            lineLabel.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel];
            
        }
        
        if (array.count !=indexPath.row+1) {
            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(50, 50-0.40, ScreenWidth-65, 0.30)];
            lineLabel1.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel1];
            
        }
        
        if (array.count ==indexPath.row+1) {
            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 50-0.40, ScreenWidth, 0.30)];
            lineLabel1.backgroundColor=qianhuiColor;
            [cell.contentView addSubview:lineLabel1];
            
        }
        
    }
    if (indexPath.section == 1) {
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-40,10, 30, 30);
        [jiantou setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];
        
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[array1 objectAtIndex:indexPath.row] objectAtIndex:1]]];
        cell.textLabel.text = [[array1 objectAtIndex:indexPath.row] objectAtIndex:0];
        
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell =(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([[user objectForKey:@"userStatus"] isEqualToString:@"E"]) {
       
        if ([cell.textLabel.text isEqualToString:@"系统设置"]){
            SystemSettingsViewController * Login=[[SystemSettingsViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
            
        }else{
        
            ToBindCompanyViewController * Login=[[ToBindCompanyViewController alloc]init];
            Login.viewSat=@"The original road";
            [self.navigationController pushViewController:Login animated:YES];
        }

    }else
        
        
    if ([[user objectForKey:@"userStatus"] isEqualToString:@"W"]) {
        
        [self requestData];
        if ([cell.textLabel.text isEqualToString:@"我的业绩"]) {
            MyresultsViewController * Login=[[MyresultsViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];}
        else if ([cell.textLabel.text isEqualToString:@"我的收藏"]) {
            DiscoverHouseholdMyCollectViewController * Login=[[DiscoverHouseholdMyCollectViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
        }else if ([cell.textLabel.text isEqualToString:@"关于我们"]) {
            AboutUsViewController * Login=[[AboutUsViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
        }else if ([cell.textLabel.text isEqualToString:@"所属门店"]){
            LoginCompanysViewController * Login=[[LoginCompanysViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
            
        }else if ([cell.textLabel.text isEqualToString:@"系统设置"]){
            SystemSettingsViewController * Login=[[SystemSettingsViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
            
        }
        
        
    }else  if ([[user objectForKey:@"userStatus"] isEqualToString:@"Y"]) {
        //通过后不需要加载
        
        if ([cell.textLabel.text isEqualToString:@"我的业绩"]) {
            MyresultsViewController * Login=[[MyresultsViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
        }else if ([cell.textLabel.text isEqualToString:@"我的收藏"]) {
            DiscoverHouseholdMyCollectViewController * Login=[[DiscoverHouseholdMyCollectViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
        }else if ([cell.textLabel.text isEqualToString:@"关于我们"]) {
            AboutUsViewController * Login=[[AboutUsViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
        }else if ([cell.textLabel.text isEqualToString:@"所属门店"]){
            LoginCompanysViewController * Login=[[LoginCompanysViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
            
        }else if ([cell.textLabel.text isEqualToString:@"系统设置"]){
            SystemSettingsViewController * Login=[[SystemSettingsViewController alloc]init];
            [self.navigationController pushViewController:Login animated:YES];
            
        }
    }
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}
- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

@end
